package com.why.aclservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.why.aclservice.entity.Permission;
import com.why.aclservice.entity.RolePermission;
import com.why.aclservice.entity.User;
import com.why.aclservice.helper.MemuHelper;
import com.why.aclservice.helper.PermissionHelper;
import com.why.aclservice.mapper.PermissionMapper;
import com.why.aclservice.service.PermissionService;
import com.why.aclservice.service.RolePermissionService;
import com.why.aclservice.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private UserService userService;


    //查询所有的菜单
    @Override
    public List<Permission> queryAllMenu() {
        //1.查询菜单表的所有数据
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("id");
        //查询到的所有数据
        List<Permission> permissionList = baseMapper.selectList(wrapper);
        //将数据按照指定需求封装
        List<Permission> resultList = buildPermissonList(permissionList);

        return resultList;
    }

    /**
     * 按指定需求封装菜单列表
     *
     * @param permissionList
     * @return
     */
    private List<Permission> buildPermissonList(List<Permission> permissionList) {


        //最终值
        List<Permission> finalResult = new ArrayList<>();
        //先找到父亲节点
        for (int i = 0; i < permissionList.size(); i++) {
            Permission permission = permissionList.get(i);
            if (permission.getPid().equals("0")) {
                //设置顶层菜单值
                permission.setLevel(1);
                Permission allChildren = selectAllChildren(permissionList, permission);
                finalResult.add(allChildren);
                break;
            }

        }


        return finalResult;
    }

    /**
     * 查询根节点对应的所有孩子
     *
     * @param permissionList
     * @param root
     * @return
     */

    private Permission selectAllChildren(List<Permission> permissionList, Permission root) {


        List<Permission> childrens = new ArrayList<>();
        root.setChildren(childrens);
        for (Permission children : permissionList) {
            //找到自己的孩子
            if (children.getPid().equals(root.getId())) {
                //设置子菜单级别
                children.setLevel(root.getLevel() + 1);
                //继续找找这个孩子对应的儿子
                Permission Child = selectAllChildren(permissionList, children);
                //最终添加到结果集
                childrens.add(Child);
            }
        }

        //返回孩子
        return root;
    }


    @Override
    public List<Permission> selectAllMenu(String roleId) {
        return null;
    }

    /**
     * 给角色分配权限
     *
     * @param roleId：角色id
     * @param permissionId：菜单id
     */
    @Override
    public void saveRolePermissionRealtionShip(String roleId, String[] permissionId) {

        //创建角色权限的集合
        List<RolePermission> rolePermissionList = new ArrayList<>();
        for (String permission : permissionId) {
            //创建对象
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permission);
            rolePermissionList.add(rolePermission);

        }
        //批量添加到角色菜单表
        this.rolePermissionService.saveBatch(rolePermissionList);


    }

    /**
     * 递归删除菜单
     *
     * @param id
     */
    @Override
    public void removeChildById(String id) {


        ArrayList<String> idList = new ArrayList<>();
        selectAllIdByRootId(id, idList);
        //传递一个id的集合，进行删除
        baseMapper.deleteBatchIds(idList);
    }

    private void selectAllIdByRootId(String id, List<String> idList) {
        if (id == null) {
            return;
        }
        idList.add(id);
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", id);
        wrapper.select("id");
        //查询所有的与父id相同的
        List<Permission> pid = baseMapper.selectList(wrapper);
        pid.forEach(item -> {
            selectAllIdByRootId(item.getId(), idList);
        });


    }


    @Override
    public List<String> selectPermissionValueByUserId(String id) {
        return null;
    }

    @Override
    public List<JSONObject> selectPermissionByUserId(String id) {
        return null;
    }
}
